var table;

$(function () {
    loadEquipmentList();
    hideModal();
});

function loadOption(data) {
    $('#store').select2({
        data: data
    });
}

/**
 * 加载设备列表
 */
function loadEquipmentList() {
    var url = baseEquipmentUrl + "list/equipment?start=0&limit=1000000";
    $.ajax({
        url: url,
        type: "GET",
        contentType: "application/json",
        success: function (data) {
            loadTable(data.data.rows);
        }
    });
    // table = $("#equipmentList").DataTable({
    //     searching: false,
    //     ordering: false,
    //     ajax: {
    //         url: baseEquipmentUrl + "list/equipment",
    //         contentType: "application/json; charset=utf-8",
    //         type: "GET",
    //         data: function (req) {
    //             req.start = req.start;
    //             req.limit = req.length;
    //         },
    //         dataSrc: function (res) {
    //             res.recordsTotal = res.data.total;
    //             res.recordsFiltered = res.data.total;
    //             return res.data.rows;
    //         }
    //     },
    //     serverSide: true,
    //     select: true,
    //     columns: [
    //         {data: 'uid'},
    //         {data: 'name'},
    //         {data: 'created'}
    //     ],
    //     pagingType: "full_numbers",
    //     language: {
    //         "sProcessing": "处理中...",
    //         "sLengthMenu": "显示 _MENU_ 项结果",
    //         "sZeroRecords": "没有匹配结果",
    //         "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
    //         "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
    //         "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
    //         "sInfoPostFix": "",
    //         "sSearch": "搜索:",
    //         "sUrl": "",
    //         "sEmptyTable": "表中数据为空",
    //         "sLoadingRecords": "载入中...",
    //         "sInfoThousands": ",",
    //         "oPaginate": {
    //             "sFirst": "首页",
    //             "sPrevious": "上页",
    //             "sNext": "下页",
    //             "sLast": "末页"
    //         },
    //         oAria: {
    //             "sSortAscending": ": 以升序排列此列",
    //             "sSortDescending": ": 以降序排列此列"
    //         }
    //     },
    //     columnDefs: [{
    //         targets: 3,
    //         render: function (data, type, row, meta) {
    //             return "<button type='button' style='margin-right: 10px;border: 1px solid #0D1113;color: #0D1113;background: transparent;' class='btn btn-primary' onclick='editEquipment(" + JSON.stringify(row) + ");'>编辑</button>";
    //         }
    //     }]
    // });
}

function loadTable(data) {
    $("#equipmentList").DataTable({
        data: data,
        searching: true,
        ordering: true,
        select: true,
        columns: [
            {data: 'uid'},
            {data: 'name'},
            {data: 'created'}
        ],
        pagingType: "full_numbers",
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            oAria: {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        },
        columnDefs: [{
            targets: 3,
            render: function (data, type, row, meta) {
                return "<button type='button' style='margin-right: 10px;border: 1px solid #0D1113;color: #0D1113;background: transparent;' class='btn btn-primary' onclick='editEquipment(" + JSON.stringify(row) + ");'>编辑</button>";
            }
        }]
    });
}

var currEquipment;

/**
 * 编辑按钮点击事件
 * @param equipment
 */
function editEquipment(equipment) {
    currEquipment = equipment;
    var url = baseStoreUrl + "list/store?start=0&limit=10000";
    $.ajax({
        url: url,
        contentType: "application/json",
        type: "GET",
        success: function (data) {
            var stores = [];
            for (var i = 0; i < data.data.rows.length; i++) {
                var store = {"id": data.data.rows[i].id, "text": data.data.rows[i].store_name};
                stores.push(store);
            }
            loadOption(stores);
            $('#editModal').modal('toggle');
        },
        error: function (data) {
            alert("错误");
        }
    });
}

/**
 * 模态框隐藏时的事件
 */
function hideModal() {
    $("#editModal").on("hide.bs.modal", function () {
        $("#store").children().remove();
    });
}

/**
 * 提交按钮点击事件
 */
function submitEdit() {
    var store = $("#store").select2("data")[0];
    if (!isNull(currEquipment) && !isNull(store)) {
        var url = baseEquipmentUrl + "update/equipment";
        $.ajax({
            url: url,
            data: JSON.stringify({"uid": currEquipment.uid, "storeId": store.id}),
            contentType: "application/json",
            type: "POST",
            success: function (data) {
                if (data.data) {
                    alert("成功");
                    $("#equipmentList").remove();
                    var tableHtml = '<table id="equipmentList" class="table table-striped table-hover" cellspacing="0" width="100%"><thead><tr><th>设备编号</th><th>所属4S店</th><th>启用时间</th><th>操作</th></tr></thead></table>';
                    $(".box-body").html(tableHtml);
                    $('#editModal').modal('hide');
                    loadEquipmentList();
                } else {
                    alert("失败");
                    $('#editModal').modal('hide');
                }
            },
            error: function (data) {
                alert("错误");
            }
        });
    }
}